<?php
use oxide\data\Connection;
use module\content\model\Content;
use oxide\ui\html;

/**
 * Controller with Admin related functions for Edu Plugin 
 */
class EduAdminController 
{
	/**
	 * additional section fieldset for admin content post for content type section
	 * @param array $args
	 */
	public function formSectionContent($args)
	{
		// initialize vars for form and content
		$content = $args['content'];
		$content_pk = $content->content_pk;	
		$form = $args['form'];
		$instructor = '';
		$location = '';
		$days = '';
		$time = '';
		$start = '';
		$end = '';
		$cost = '';
		$status = 0;
		
		
		// if content pk exists, ie. there is existing record, 
		// show the following fields with the content metadata
		if($content_pk) {
			// if content metadata has value, set vars to display to input controls
			$metadata = $content->metadata();
			$instructor = $metadata->EduPlugin_instructor;
			$location = $metadata->EduPlugin_location;
			$days = $metadata->EduPlugin_days;
			$time = $metadata->EduPlugin_time;
			$start = $metadata->EduPlugin_start;
			$end = $metadata->EduPlugin_end;
			$cost = $metadata->EduPlugin_cost;
			$status = $metadata->EduPlugin_status;
		}

		// add controls for the section fieldset and form
		$fieldset = 'Section:';
		$status_options = array(1 => 'Active', 0 => 'Inactive');
		$form->addControl(new html\InputControl('text', 'EduPlugin_instructor', $instructor, 'Instructor:'), $fieldset);
		$form->addControl(new html\InputControl('text', 'EduPlugin_location', $location, 'Location:'), $fieldset);
		$form->addControl(new html\InputControl('text', 'EduPlugin_days', $days, 'Days:'), $fieldset);
		$form->addControl(new html\InputControl('text', 'EduPlugin_time', $time, 'Time:'), $fieldset);
		$form->addControl(new html\InputControl('date', 'EduPlugin_start', $start, 'Start:'), $fieldset);
		$form->addControl(new html\InputControl('date', 'EduPlugin_end', $end, 'End:'), $fieldset);
		$form->addControl(new html\InputControl('text', 'EduPlugin_cost', $cost, 'Cost:', array('size' => '10')), $fieldset);
		$form->addControl(new html\SelectControl('EduPlugin_status', $status, 'Course Status', null, $status_options), $fieldset);

	}
	
	/**
	 * save the section fieldset values upon submitted
	 * @param type $args 
	 */
	public function saveSectionContentValues($args)
	{
		// initialize vars with content and values
		$content = $args['content'];
		$metadata = $content->metadata();
		$values = $args['values'];
		
		// if fields are not empty, save the input values
		// if not, save values as null
		!empty($values['EduPlugin_instructor']) ? $metadata->EduPlugin_instructor = $values['EduPlugin_instructor'] : null;
		!empty($values['EduPlugin_location']) ? $metadata->EduPlugin_location = $values['EduPlugin_location'] : null;
		!empty($values['EduPlugin_days']) ? $metadata->EduPlugin_days = $values['EduPlugin_days'] : null;
		!empty($values['EduPlugin_time']) ? $metadata->EduPlugin_time = $values['EduPlugin_time'] : null;
		!empty($values['EduPlugin_start']) ? $metadata->EduPlugin_start = $values['EduPlugin_start'] : null;
		!empty($values['EduPlugin_end']) ? $metadata->EduPlugin_end = $values['EduPlugin_end'] : null;
		!empty($values['EduPlugin_cost']) ? $metadata->EduPlugin_cost = $values['EduPlugin_cost'] : null;
		!empty($values['EduPlugin_status']) ? $metadata->EduPlugin_status = $values['EduPlugin_status'] : null;
	}
	
	/**
	 * display the content for the instructors with specified fieldsets and controls
	 * The main content fields for instrcutors include: title, description, detail and tags
	 * The misc contain fields: parent_pk, permalink, created on, modified on, and status
	 * @param type $args 
	 */
	public function formInstructorContent($args)
	{
		$form = $args['form'];
		$content = $args['content'];
		$description_value = null;
		if (!empty($content->content_filtered)) { 
			$description_value = $content->content_filtered; 
		}
		
		// change the title of the following controls
		$form->getControl('content')->setLabel('Detail:');
		
		// remove these following controls from form
		$title_control = $form->removeControl('title');	
		$content_control = $form->removeControl('content');
		$status_control = $form->removeControl('status');
		$tags_control = $form->removeControl('tags');
		
		// place the removed controls within the fieldset
		$type_fieldset = "Content Info:";
		$description_control = new oxide\ui\html\TextareaControl('description', $description_value, 'Description');
		$form->addControl($title_control, $type_fieldset);
		$form->addControl($description_control, $type_fieldset);
		$form->addControl($content_control, $type_fieldset);
		$form->addControl($status_control, $type_fieldset);
		$form->addControl($tags_control, $type_fieldset);
		
		
		// remove the following controls from form
		$parent_pk = $form->removeControl('parent_pk');
		$permalink = $form->removeControl('permalink');
		$created_on = $form->removeControl('created_on');
		$modified_on = $form->removeControl('modified_on');
		
		// place the removed controls within the fieldset
		$fieldset = "Misc:";
		$form->addControl($parent_pk, $fieldset);
		$form->addControl($permalink, $fieldset);
		$form->addControl($created_on, $fieldset);
		$form->addControl($modified_on, $fieldset);
	}
	
	
	/**
	 * override content filtered value with the value received from description field input
	 * @param type $args 
	 */
	public function willSaveInstructorContent($args)
	{
		$content = $args['content'];
		$values = $args['values'];
		
		// override the content filtered with values from description control
		if($values['description']) { 
			$content->content_filtered = $values['description']; 
		}
	}

	
	/**
	 * override admin user entry form with field / fieldset
	 * @param type $args 
	 */
	public function formAdminUserEntry($args)
	{
		$form = $args['form'];
		$user = $args['user'];
		
		// find all courses that the user have registered for
		$courses = $this->findRegisteredUserSection($user->user_pk);
		
		if (empty($courses)) { return; }
		
		// add controls for the section fieldset and form
		$fieldset = 'Registration Info:';
		$status_options = EduPlugin::getAvailableUserSectionStatus();
		foreach ($courses as $course) {
			if (!is_null($course['edu_status'])) {
				
				$content_pk = $course['content_pk'];
				$content_title = $course['title'];

				$form->addControl(new html\StaticControl('p', "EduCourseTitle[$content_pk]", $content_title, 'Course Title'), $fieldset);
				$form->addControl(new html\SelectControl("EduStatus[$content_pk]", $course['edu_status'], 'Status', null, $status_options), $fieldset);
				
			}
		}
	}
	
	/**
	 * save the registration info fieldset values upon submitted
	 * @param type $args 
	 */
	public function saveRegistrationInfoValues($args)
	{
		// initialize
		$user = $args['user'];
		$values = $args['values'];
		$edu_contents = $values['EduStatus'];
		
		// update the user's registered course status
		foreach ($edu_contents as $content_pk => $status) {
			$cue = \module\content\model\ContentUserExtension::findByContent_pkAndUser_pk($content_pk, $user->user_pk);
			$cue->edu_status = $status;
			$cue->save();
		}
	}
	
	
	/**
	 * filter the list query based on criterias
	 * @param type $args
	 * @param type $request 
	 */
	public function queryUserAdminListQuery($args, $request)
	{
		// initialize
		$edu_section = $request->getQuery('edu_section', null);
		$edu_status = $request->getQuery('edu_status', null);
		$query = $args['query'];
		
		// set the join for content user extension and user table
		$query->join('content_user_extension cue', array('cue.user_pk' => 'user.user_pk'), oxide\data\Connection::JOIN_LEFT);
		
		// check for user registered course
		if($edu_section) {	
			$query->where('cue.content_pk', $edu_section);
		}

		// check for status of user registered course
		if($edu_status) {
			$query->where('cue.edu_status', $edu_status);
		}
	}
	
	
	/**
	 * find the content and title that user course status as active
	 * @param int $user_pk
	 * @return array
	 */
	private function findRegisteredUserSection($user_pk)
	{
		$query = new \oxide\data\sql\SelectQuery('content_user_extension', Connection::sharedInstance());
		$query->columns(array('content_user_extension.content_user_extension_pk', 'content_user_extension.content_pk', 'content_user_extension.edu_status', 'content.title'));
		$query->where('content_user_extension.user_pk', $user_pk, \oxide\data\Connection::OP_EQ);
		$query->join('content', array('content.content_pk' => 'content_user_extension.content_pk'), \oxide\data\Connection::JOIN_LEFT);
		$stmt = \module\content\model\ContentUserExtension::select($query);
		return $stmt->fetchAll();
	}
}
?>